from sqlalchemy.ext.asyncio import AsyncSession
from typing import List
from config.constant import CommonConstant
from exceptions.exception import ServiceException
from module_admin.dao.kh_monthly_attendance_dao import Kh_monthly_attendanceDao
from module_admin.entity.vo.common_vo import CrudResponseModel
from module_admin.entity.vo.kh_monthly_attendance_vo import Kh_monthly_attendancePageQueryModel, \
    Kh_monthly_attendanceModel, DeleteKh_monthly_attendanceModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Kh_monthly_attendanceService:
    """
    考勤月报表模块服务层
    """

    @classmethod
    async def get_kh_monthly_attendance_list_services(
        cls, query_db: AsyncSession, query_object: Kh_monthly_attendancePageQueryModel, is_page: bool = False
    ):
        """
        获取考勤月报表列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 考勤月报表列表信息对象
        """
        kh_monthly_attendance_list_result = await Kh_monthly_attendanceDao.get_kh_monthly_attendance_list(query_db, query_object, is_page)

        return kh_monthly_attendance_list_result


    @classmethod
    async def add_kh_monthly_attendance_services(cls, query_db: AsyncSession, page_object: Kh_monthly_attendanceModel):
        """
        新增考勤月报表信息service

        :param query_db: orm对象
        :param page_object: 新增考勤月报表对象
        :return: 新增考勤月报表校验结果
        """
        try:
            await Kh_monthly_attendanceDao.add_kh_monthly_attendance_dao(query_db, page_object)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message='新增成功')
        except Exception as e:
            await query_db.rollback()
            raise e

    @classmethod
    async def edit_kh_monthly_attendance_services(cls, query_db: AsyncSession, page_object: Kh_monthly_attendanceModel):
        """
        编辑考勤月报表信息service

        :param query_db: orm对象
        :param page_object: 编辑考勤月报表对象
        :return: 编辑考勤月报表校验结果
        """
        edit_kh_monthly_attendance = page_object.model_dump(exclude_unset=True, exclude={'del_flag', 'create_by', 'create_time', })
        kh_monthly_attendance_info = await cls.kh_monthly_attendance_detail_services(query_db, page_object.id)
        if kh_monthly_attendance_info.id:
            try:
                await Kh_monthly_attendanceDao.edit_kh_monthly_attendance_dao(query_db, edit_kh_monthly_attendance)
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='更新成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='考勤月报表不存在')

    @classmethod
    async def delete_kh_monthly_attendance_services(cls, query_db: AsyncSession, page_object: DeleteKh_monthly_attendanceModel):
        """
        删除考勤月报表信息service

        :param query_db: orm对象
        :param page_object: 删除考勤月报表对象
        :return: 删除考勤月报表校验结果
        """
        if page_object.ids:
            id_list = page_object.ids.split(',')
            try:
                for id in id_list:
                    await Kh_monthly_attendanceDao.delete_kh_monthly_attendance_dao(query_db, Kh_monthly_attendanceModel(id=id))
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='删除成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='传入序号为空')

    @classmethod
    async def kh_monthly_attendance_detail_services(cls, query_db: AsyncSession, id: int):
        """
        获取考勤月报表详细信息service

        :param query_db: orm对象
        :param id: 序号
        :return: 序号对应的信息
        """
        kh_monthly_attendance = await Kh_monthly_attendanceDao.get_kh_monthly_attendance_detail_by_id(query_db, id=id)
        if kh_monthly_attendance:
            result = Kh_monthly_attendanceModel(**CamelCaseUtil.transform_result(kh_monthly_attendance))
        else:
            result = Kh_monthly_attendanceModel(**dict())

        return result

    @staticmethod
    async def export_kh_monthly_attendance_list_services(kh_monthly_attendance_list: List):
        """
        导出考勤月报表信息service

        :param kh_monthly_attendance_list: 考勤月报表信息列表
        :return: 考勤月报表信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'id': '序号',
            'userName': '用户姓名',
            'userId': '用户编号',
            'khMonth': '考勤月份',
            'amPm': '考勤时段',
            'khWork': '考勤类型',
            'day1': '1日',
            'day2': '2日',
            'day3': '3日',
            'day4': '4日',
            'day5': '5日',
            'day6': '6日',
            'day7': '7日',
            'day8': '8日',
            'day9': '9日',
            'day10': '10日',
            'day11': '11日',
            'day12': '12日',
            'day13': '13日',
            'day14': '14日',
            'day15': '15日',
            'day16': '16日',
            'day17': '17日',
            'day18': '18日',
            'day19': '19日',
            'day20': '20日',
            'day21': '21日',
            'day22': '22日',
            'day23': '23日',
            'day24': '24日',
            'day25': '25日',
            'day26': '26日',
            'day27': '27日',
            'day28': '28日',
            'day29': '29日',
            'day30': '30日',
            'day31': '31日',
            # 'delFlag': '删除标志',
            # 'createBy': '创建者',
            # 'createTime': '创建时间',
            # 'updateBy': '更新者',
            # 'updateTime': '更新时间',
            # 'remark': '备注',
        }
        binary_data = ExcelUtil.export_list2excel(kh_monthly_attendance_list, mapping_dict)

        return binary_data
